package com.idev.common.util;

import com.idev.common.entiy.SysEmail;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * Created by yigeyanse on 2017/6/20.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Component
public class SendEmailUtil {

    public static SendEmailUtil sendEmailUtil;

    @PostConstruct
    public void init() {
        sendEmailUtil = this;
    }

    @Autowired
    private JavaMailSender mailSender; //自动注入的Bean

    @Value("${spring.mail.username}")
    private String sender; //读取配置文件中的参数

    @Autowired
    private ThymeLeafTemplateMail templateMail;

    /**
     * 发送简单文本邮件
     * @param mail
     * @throws Exception
     */
    public static void sendSimpleMail(SysEmail mail) throws Exception {
        SimpleMailMessage message = new SimpleMailMessage();
        if(StringUtils.isEmpty(mail.getFrom()))
            message.setFrom(sendEmailUtil.sender);
        else
            message.setFrom(mail.getFrom());
        message.setTo(mail.getTo()); //自己给自己发送邮件
        message.setSubject(mail.getSubject());
        message.setText(mail.getContents());
        sendEmailUtil.mailSender.send(message);
    }

    /**
     * 发送Html邮件
     * @param mail
     */
    public static void sendHtmlMail(SysEmail mail) {
        MimeMessage message = null;
        try {
            message = sendEmailUtil.mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendEmailUtil.sender);
            helper.setTo(mail.getTo());
            helper.setSubject(mail.getSubject());
            helper.setText(mail.getContents(), true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        sendEmailUtil.mailSender.send(message);
    }

    /**
     * 发送带附件的邮件
     * @param receiver 接收者
     */
    public static void sendAttachmentsMail(String receiver) {
        MimeMessage message = null;
        try {
            message = sendEmailUtil.mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendEmailUtil.sender);
            helper.setTo(receiver);
            helper.setSubject("主题：带附件的邮件");
            helper.setText("带附件的邮件内容");
            //注意项目路径问题，自动补用项目路径
            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
            //加入邮件
            helper.addAttachment("图片.jpg", file);
        } catch (Exception e){
            e.printStackTrace();
        }
        sendEmailUtil.mailSender.send(message);
    }

    /**
     * 发送带静态资源的邮件
     * @param receiver 接收者
     */
    public static void sendInlineMail(String receiver) {
        MimeMessage message = null;
        try {
            message = sendEmailUtil.mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendEmailUtil.sender);
            helper.setTo(receiver);
            helper.setSubject("主题：带静态资源的邮件");
            //第二个参数指定发送的是HTML格式,同时cid:是固定的写法
            helper.setText("<html><body>带静态资源的邮件内容 图片:<img src='cid:picture' /></body></html>", true);

            FileSystemResource file = new FileSystemResource(new File("src/main/resources/static/image/picture.jpg"));
            helper.addInline("picture",file);
        } catch (Exception e){
            e.printStackTrace();
        }
        sendEmailUtil.mailSender.send(message);
    }

    /**
     * 发送模板邮件
     * @param mail
     * @throws Exception
     */
    public static void sendTemplateMail(SysEmail mail) throws Exception {
        try {
            sendEmailUtil.templateMail.setModel(mail.getData());
            sendEmailUtil.templateMail.setFrom(sendEmailUtil.sender);
            sendEmailUtil.templateMail.setToEmail(mail.getTo());
            sendEmailUtil.templateMail.setSubject(mail.getSubject());
            sendEmailUtil.templateMail.setTemplateName(mail.getTemplateName());
            sendEmailUtil.templateMail.sendEmail();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
